// ==================================================================================
// Shared Genomics Project MPI Codebase
// Version 1.0 30/04/2010
//
// (c) 2010 University of Manchester all rights reserved
//
// This file is distributed under the GNU General Public License, Version 2.  
// Please see the file COPYING.txt for more details
// ==================================================================================

/*!
\file 
\brief Main body of the Strata or Layer (SOL) program
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "getopt.h"
#include "mt19937ar.h"
#include "options.h"
#include "seedfile.h"
#include "solfile.h"

struct option long_options[] = {
	{ "id", required_argument, NULL, 'i' }, // Run ID
	{ "covar", required_argument, NULL, 'c' }, // Covariant file a.k.a. PHE file.
	{ "covar-number", required_argument, NULL, 'n' }, // index to the single co-variant column, i.e. 1->N where N = No. columns.
	{ "ldir", required_argument, NULL, 'l'}, // Local output direct
	{ "rdir", required_argument, NULL, 'r'}, // Remote output direct
	{ "include-missing", optional_argument, NULL, 'k'}, // Include missing level (PLINK compatibility option).
	{ "nperm", required_argument, NULL, 'p'}, // No. of permutations
	{ "covar-name", required_argument, NULL, 'C'}, // Covariant column name
	{ 0,    0,    0,    0   } 
};

#define NREQUIRED_ARGS 5

// Create a strata/layer PHE file for 'set' permute algorithm.
int solcol_main(int argc, char *argv[]) {
	char ch;
	struct selected_options ops;
	int nRequiredArgs = NREQUIRED_ARGS;
	BOOL skip_missing = TRUE;
	unsigned long seed = (unsigned long) time(NULL);
	BOOL jobDone = FALSE;
	
	options_init(&ops);
	init_genrand(seed);
	
	while ((ch = (char) getopt_long(argc, (const char**) argv, "m:v:p:f:i:", long_options, NULL)) != -1) {
		switch (ch) {
			case 'i': // Run ID
				strcpy(ops.szRunId, optarg);
				nRequiredArgs--;
				break;
			case 'c': //
				strcpy(ops.szPheFileName, optarg);
				nRequiredArgs--;
				break;
			case 'n':
				ops.mpheno  = atoi(optarg);
				if (ops.mpheno >= 0) nRequiredArgs--;
				break;
			case 'l': // Local working directory.
				strcpy(ops.ldir, optarg);
				nRequiredArgs--;
				break;
			case 'r': // Remote working directory.
				strcpy(ops.rdir, optarg); // Files read mostly straight off the file server.
				nRequiredArgs--;
				break;
			case 'k': // Include missing level values in the output PHE file.
				skip_missing = FALSE;
				break;
			case 'p': // Number of permutations.
				ops.nperm = atoi(optarg);
				if (ops.nperm <= 0) ops.nperm = DEFAULT_NPERM;
				break;
			case 'C': // Covariant column-name
				strcpy(ops.szSolColumnName, optarg);
				nRequiredArgs--;
				break;
			default:
				break;
		}
	}

	if (nRequiredArgs != 0) return -1;
	if (solfile_create(&ops, skip_missing)) { 
		rseed_log(&ops, seed);
		jobDone = TRUE;
	}
	
	if (jobDone) return 0;
	else return -1;
}

int main(int argc, char *argv[]) {
	return solcol_main(argc, argv);
}
